package LinkedList;

public class GetIntersectionNode {
    public static void main(String[] args) {

    }
    public ListNode getIntersectionNode(ListNode headA,ListNode headB){
        ListNode curA=headA;
        ListNode curB=headB;
        int lenA=0,lenB=0;
        while(curA!=null){
            lenA++;
            curA=curA.next;
        }
        while(curB!=null){
            lenB++;
            curB=curB.next;
        }
        curA=headA;
        curB=headB;
        //如果b链表长，让a与b交换
        if(lenA<lenB){
            int temp=lenA;
            lenA=lenB;
            lenB=temp;
            ListNode tempNode=curA;
            curA=curB;
            curB=tempNode;
        }
        int diffLen=lenA-lenB;
        for(int i=0;i<diffLen;i++){
            curA=curA.next;
        }
        while(curA!=null && curB!=null){
            //注意跟cur的value没关系，现在是判断是不是相交
            //相交的话指针指的就是同一个节点
            if(curA==curB){
                return curA;
            }
            curA=curA.next;
            curB=curB.next;
        }
        return null;
    }
}
